package com.love.poetry.interfaces;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.love.poetry.application.KeywordTagService;
import com.love.poetry.domain.KeywordTag;
import com.youboy.core.orm.Page;
import com.youboy.core.web.controller.CrudControllerSupport;
import com.youboy.util.EntityUtils;

@Controller
@RequestMapping(value="/keywordtag")
public class KeywordTagController extends CrudControllerSupport<String, KeywordTag>{
	
	@Autowired
	private KeywordTagService keywordTagService;
	
	private static final String REDIRECT_LIST = "redirect:/keywordtag/list/";
	@Override
	protected String getViewPackage(){
		return "/keywordtag";
	}
	
	@Override
	@RequestMapping(value="/create",method=RequestMethod.GET)
	public String create(Model model) {
		model.addAttribute(new KeywordTag()).addAttribute("keywordTagNames", keywordTagService.query(""));
		return formView();
	}
	
	@Override
	public String create(KeywordTag arg0, BindingResult arg1) {
		return null;
	}
	
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public String create(KeywordTag entity, BindingResult result,HttpServletRequest request) {
		if(result.hasErrors()){
			System.out.println("keywordtag:create error");
			return null;
		}
		KeywordTag fatherKeywordTag = keywordTagService.get(request.getParameter("belongKeywordTag"));
        entity.setFather(fatherKeywordTag);
		keywordTagService.save(entity);
		return REDIRECT_LIST;
	}

	@Override
	@RequestMapping(value="/{id}/delete/",method=RequestMethod.DELETE)
	public String delete(@PathVariable("id")String id) {
		try {
			keywordTagService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listView();
	}

	@Override
	@RequestMapping(value="/delete/",method=RequestMethod.DELETE)
	public String delete(HttpServletRequest request) {
        String[] items = EntityUtils.nullSafe(request.getParameterValues( "items"), new String[] {});
        
        for (String item : items) {
           delete(item);
       }
		return listView();
	}

	@Override
	@RequestMapping(value="/{id}/edit/",method=RequestMethod.GET)
	public String edit(@PathVariable("id")String id, Model model) {
		KeywordTag category=keywordTagService.get(id);
		model.addAttribute(category).addAttribute("keywordTagNames", keywordTagService.query("")).addAttribute("_method", "PUT");
		return formView();
	}

	@Override
	@RequestMapping(value="/{id}/edit/",method=RequestMethod.PUT)
	public String edit(@PathVariable("id")String id, HttpServletRequest request) {
        try {
                   KeywordTag entity = keywordTagService.get(id);
                   entity.setFather(keywordTagService .get(request.getParameter( "belongKeywordTag")));
                   bind(request, entity);
                   keywordTagService.update(entity);
                   success("标签修改成功" );
        } catch (Exception e) {
			e.printStackTrace();
			return null;
		}
        return REDIRECT_LIST ;
	}

	@RequestMapping(value="/list/",method=RequestMethod.GET)
	public String list(Page<KeywordTag> page,Model model){
		model.addAttribute(keywordTagService.queryPage(page));
		return listView();
	}
}